package com.bdqn.mr.chapter02;

import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

public class FlowReducer extends Reducer<Text, FlowBean, Text, FlowBean> {
    // 输出的结果：
    // 13560436666     1116     954      2070
    // 手机号码        上行流量 下行流量 总流量
    private final static FlowBean flowBean = new FlowBean();

    @Override
    protected void reduce(Text key, Iterable<FlowBean> values, Context context) throws IOException, InterruptedException {

        Long totalUp = 0L;
        Long totalDown = 0L;
        for (FlowBean flow : values) {
            totalUp += flow.getUpFlow();
            totalDown += flow.getDownFlow();
        }

        flowBean.setUpFlow(totalUp);
        flowBean.setDownFlow(totalDown);
        flowBean.setTotalFlow();

        context.write(key, flowBean);
    }
}
